/****************************************************
          File Name:opendir_family.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue Dec  9 14:45:01 2014
*****************************************************/
// can ONLY works fine in cwd i.e. in "."
#include "dir.h"

void do_parse(const char *name)
{
    struct stat sat;
    stat(name, &sat);
    
    switch(sat.st_mode & S_IFMT)
    {
        case S_IFDIR:
            printf("d\t");
            break;
        default:
            printf("-\t");
            break;
    }
}

int main(int argc, char *argv[])
{
    DIR *dirp = NULL;
    struct dirent *dp = NULL;
    struct stat sat;
    
    if(argc < 2)
    {
        fprintf(stderr, "%s pathname\n", argv[0]);
        exit(1);
    }
    
    dirp = opendir(argv[1]);
    if(dirp == NULL)
         err("opendir");

   // dp = (struct dirent *) malloc(sizeof(struct dirent));
   // bzero(dp, sizeof(dp));

    while((dp = readdir(dirp)) != NULL)
    {
        if(dp->d_name[0] != '.')
        {
            do_parse(dp->d_name);
            printf("%s\n", dp->d_name);
        }
    }
    putchar('\n'); 

    if(closedir(dirp) == -1)
        err("closedir");

    return 0;
}
